<?php

/**
 * WSB_Panel
 *
 * PHP version 5
 *
 * @copyright  2007 Zephir Software Design AG
 */

require_once(WSB_MODULES_PATH . 'cms/Url.php');

class WSB_Panel
{

	/**
	 * @var integer
	 */
    private $_modulesCount = 0;

    /**
     * @param integer $count
     */
    public function setContainerCount($count)
	{
        $this->_modulesCount = $count;
    }

    /**
     * @param Cms_Model_Container $container
     * @param WSB_Xtension_Base $XT
     * @return string
     */
	public function buildPageContainerPanel(Cms_Model_Container $container, WSB_Xtension_Base $XT)
	{
		$links = array();
		// Edit Icon
		if($XT->isEditable()) {
			array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::miniconEdit(), Cms_Url::xtensionEditorUrl($container->id, $XT, $_SERVER['REQUEST_URI'])));
		} else {
			array_push($links, WSB_Icon::cleargif());
		}
		// Add Icon
		$url = Cms_Url::containerSelectModuleUrl($container->page_id, $container->placeholder_id, $container->sort+1, $container->language_id);
		array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::miniconAdd(), $url));
		// Delete Icon
		$url = Cms_Url::containerDeleteContainerUrl($container->id, $container->page_id);
		//array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::miniconDelete(), $url));
		array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::miniconDelete(), "javascript:wsbYesNoDialog('".$url."','".$container->id."');"));
		if(!empty($this->_modulesCount)) {
        	// Move Up Icon
        	if($container->sort > 0) {
        		$url = Cms_Url::containerMoveUpUrl($container->page_id, $container->id);
        		array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::miniconUp(), $url));
        	} else {
        		array_push($links, WSB_Icon::cleargif());
        	}
        	// Move Down Icon
        	if($container->sort < ($this->_modulesCount-1)) {
        		$url = Cms_Url::containerMoveDownUrl($container->page_id, $container->id);
        		array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::miniconDown(), $url));
        	} else {
        		array_push($links, WSB_Icon::cleargif());
        	}
		}

		// Konfiguration
		array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::miniconConfiguration(), Cms_Url::containerConfigurationUrl($container->id)));

		// Hidden
		if(empty($container->hidden)) {
			array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::visible(), Cms_Url::containerHideUrl($container->page_id, $container->id)));
		} else {
			array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::hidden(), Cms_Url::containerUnhideUrl($container->page_id, $container->id)));
		}
		return sprintf('<div class="container-editpanel">%s</div>', implode('&nbsp;', $links));
	}

	/**
	 * @param Cms_Model_Container $container
	 * @param WSB_Xtension_Base $XT
	 * @return string
	 */
	public function buildFrontendPanel(Cms_Model_Container $container, WSB_Xtension_Base $XT)
	{
		$translate = Zend_Registry::get('Zend_Translate');

		$links = array();
		$attributes = array();
		// Edit
		if($XT->isEditable()) {
		    $url = Cms_Url::xtensionEditorUrl($container->id, $XT, $_SERVER['REQUEST_URI'], true);
			array_push($links, WSB_Controller_Url::getHtmlPopupLink($translate->_('edit'), $url, null, null, $attributes));
		} else {
            array_push($links, sprintf('<span>%s</span>', $translate->_('edit')));
        }

        // Add
		$url = Cms_Url::containerSelectModuleUrl($container->page_id, $container->placeholder_id, $container->sort+1, $container->language_id, true);
		array_push($links, WSB_Controller_Url::getHtmlPopupLink($translate->_('add'), $url, null, null, $attributes));

		// Delete
		$url = Cms_Url::containerDeleteContainerUrl($container->id, $container->page_id, true);
		array_push($links, WSB_Controller_Url::getHtmlPopupLink($translate->_('delete'), $url, null, 150, $attributes));

		// Hidden
		if(empty($container->hidden)) {
			$url = Cms_Url::containerHideUrl($container->page_id, $container->id, $_SERVER['REQUEST_URI']);
			array_push($links, WSB_Controller_Url::getHtmlLink($translate->_('hide'), $url, $attributes));
			//array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::visible(), Cms_Url::containerHideUrl($container->page_id, $container->id)));
		} else {
			$url = Cms_Url::containerUnhideUrl($container->page_id, $container->id, $_SERVER['REQUEST_URI']);
			array_push($links, WSB_Controller_Url::getHtmlLink($translate->_('unhide'), $url, $attributes));
			//array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::hidden(), Cms_Url::containerUnhideUrl($container->page_id, $container->id)));
		}

		// Konfiguration
		array_push($links, WSB_Controller_Url::getHtmlPopupLink($translate->_('configuration'), Cms_Url::containerConfigurationUrl($container->id, true)));

		if(!empty($this->_modulesCount)) {
        	// Move Up
        	if($container->sort > 0) {
        		$url = Cms_Url::containerMoveUpUrl($container->page_id, $container->id, $_SERVER['REQUEST_URI']);
        		array_push($links, WSB_Controller_Url::getHtmlLink($translate->_('moveUp'), $url, $attributes));
        	} else {
        		array_push($links, sprintf('<span>%s</span>', $translate->_('moveUp')));
        	}
        	// Move Down
        	if($container->sort < ($this->_modulesCount-1)) {
        		$url = Cms_Url::containerMoveDownUrl($container->page_id, $container->id, $_SERVER['REQUEST_URI']);
        		array_push($links, WSB_Controller_Url::getHtmlLink($translate->_('moveDown'), $url, $attributes));
        	} else {
        		array_push($links, sprintf('<span>%s</span>', $translate->_('moveDown')));
        	}
		}

		return sprintf('<div class="wsb-editpanel-links"><div>%s</div></div>', implode('<span class="delim">|</span>', $links));
	}

}
